package com.zzy.librarytest.web;

import com.zzy.librarytest.entity.Book;
import com.zzy.librarytest.service.impl.BookServviceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Book操作
 */
@Controller
public class BookController {
    @Autowired
    private BookServviceImpl bookServvice;
    @PostMapping("/homepage/addbook")
    public String addBook(Book book, Model model){
        Integer book1 = bookServvice.saveBook(book);
        if (book1!=-1){
            model.addAttribute("msg","添加成功");
            return "redirect:/homepage";
        }else {
            model.addAttribute("msg","添加失败");
            return "book/addbook";
        }
    }
    @GetMapping("/homepage/selectbook")
    public String selectBook(Integer id,Model model){
        Book book = bookServvice.selectBook(id);
        if (book!=null){
            model.addAttribute("book",book);
            return "book/deletebook";
        }else {
            model.addAttribute("msg","编号为:"+id+" 书不存在");
            return "book/deletebook";
        }
    }
    @GetMapping("/homepage/deletebook")
    public String deleteBook(Book book,Model model){
        Integer integer = bookServvice.deleteBook(book.getId());
        if (integer!=-1){
            model.addAttribute("msg","删除成功");
            return "book/deletebook";
        }else {
            model.addAttribute("msg","删除失败");
            return "book/deletebook";
        }
    }
    @GetMapping("/homepage/selectbook2")
    public String selectBook2(Integer id,Model model){
        Book book = bookServvice.selectBook(id);
        if (book!=null){
            model.addAttribute("book",book);
            return "book/selectbook";
        }else {
            model.addAttribute("msg","编号为:"+id+" 书不存在");
            return "book/selectbook";
        }
    }
    @GetMapping("/homepage/selectbook3")
    public String selectBook3(Integer id,Model model){
        Book book = bookServvice.selectBook(id);
        if (book!=null){
            model.addAttribute("book",book);
            return "book/updatebook";
        }else {
            model.addAttribute("msg","编号为:"+id+" 书不存在");
            return "book/updatebook";
        }
    }
    @PostMapping("/homepage/updatebook")
    public String updateBook(Book book,Model model){
        Integer integer = bookServvice.updateBook(book);
        if (integer!=-1){
            model.addAttribute("msg","修改成功");
            Book book1 = bookServvice.selectBook(book.getId());
            model.addAttribute("book",book1);
            return "book/updatebook";
        }else {
            model.addAttribute("msg","修改失败");
            return "book/updatebook";
        }
    }
}
